package org.ajie.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.ajie.entity.SysUser;
import org.ajie.model.PageParams;
import org.ajie.model.Result;
import org.ajie.service.SysUserService;
import org.ajie.utils.PasswordUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysUser")
@Tag(name="用户管理")
@ApiSupport(author = "xm:2383566697@qq.com",order = 1)
public class SysUserController {

    @Resource
    private SysUserService userService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @GetMapping("info/{id}")
    @Operation(summary = "通过ID查询单条数据",parameters = {
            @Parameter(name = "id",description = "主键",required = true,in = ParameterIn.PATH)
    })
    public Result<SysUser> queryById(String id){
        return Result.success(userService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param sysUser 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @PostMapping("page")
    @Operation(summary = "分页查询")
    public Result<Page<SysUser>> pageQuery(@RequestBody PageParams<SysUser> pageParams){
        SysUser params = pageParams.getParams();
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(params.getNickname()),SysUser::getNickname, params.getNickname());
        return Result.success(userService.page(pageParams.getPage(), lqw));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    @Operation(summary = "新增数据")
    public Result<Boolean> add(@RequestBody SysUser sysUser){
        String slat = RandomUtil.randomString(16);
        sysUser.setSlat(slat);
        String password = sysUser.getPassword();
        String encrypt = PasswordUtils.encrypt(slat, password);
        sysUser.setPassword(encrypt);
        if (userService.save(sysUser)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @PostMapping("update")
    @Operation(summary = "更新数据")
    public Result<SysUser> edit(@RequestBody SysUser sysUser){
        if (userService.updateById(sysUser)){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @GetMapping("/delete/{id}")
    @Operation(summary = "通过主键删除数据",parameters = {
            @Parameter(name = "id",description = "主键",required = true,in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        if (userService.removeById(id)){
            return Result.success();
        }
        return Result.error();
    }
}
